import ResourceNameForTable from '@/components/resourceLibraryComp/ResourceNameForTable';
import { IResourceNameParam } from '@/components/resourceLibraryComp/ResourceNameForTable/interface';
import type { ColumnType } from 'antd/es/table';
import Source from '../../Entity/Source';

type Column = ColumnType<Source>;

const nameWidth = 160;
const timeWidth = 160;
const classWidth = 200;

const DICT: Record<Source['type'], IResourceNameParam['resourceFrom']> = {
  pdf课件: 'pdfCourseWare',
  判断题: 'judgeQue',
  单选题: 'sChoiceQue',
  多选题: 'mChoiceQue',
  图文课件: 'graphicCourseWare',
  视频课件: 'videoCourseWare',
  试卷: 'testPaper',
  课件包: 'courseWarePack',
  未知: 'courseWarePack',
};

const NameColumn: Column = {
  title: '名称',
  dataIndex: 'name',
  width: 240,
  render(value, record) {
    return (
      <ResourceNameForTable title={value} resourceType={Number(record.isFolder)} resourceFrom={DICT[record.type]} />
    );
  },
};
const TradeColumn: Column = {
  title: '行业',
  dataIndex: ['origin', 'trade'],
  width: nameWidth,
  ellipsis: true,
};
const ClassColumn: Column = {
  title: '所属分类',
  dataIndex: ['origin', 'className'],
  width: classWidth,
  ellipsis: true,
};
// TODO: 0711 zyj 改dataIndex:['updateTime','createTime'] 为 dataIndex: 'updateTime'
const UpdateTimeColumn: Column = {
  title: '修改时间',
  dataIndex: 'updateTime',
  sorter: true,
  width: timeWidth,
};
const CreatTimeColumn: Column = {
  title: '修改时间',
  dataIndex: 'createTime',
  sorter: true,
  width: timeWidth,
};
const IsChargeColumn: Column = {
  title: '是否收费',
  dataIndex: 'isCharge',
  width: 80,
  render(value, record) {
    return record.isCharge == 1 ? '是' : '否';
  },
};
/**
 * @desc 平台发布资源预设，原始资源预设，政府课件包预设
 */
type ColumnPresets = 'plat' | 'origin' | 'gover_pkg';

export function getColumns(key: ColumnPresets) {
  if (key === 'plat') {
    return [NameColumn, TradeColumn, ClassColumn, UpdateTimeColumn];
  } else if (key === 'gover_pkg') {
    return [NameColumn, ClassColumn, UpdateTimeColumn];
  } else if (key === 'origin') {
    return [NameColumn, UpdateTimeColumn];
  } else {
    return [];
  }
}

type ColumnPicks = 'name' | 'trade' | 'class' | 'update' | 'creat' | 'isCharge';

/**
 * @desc 从指定的列配置中选择使用哪几种
 */
export function PickColumns(pick: ColumnPicks[], columnsRender?: { tradeMap: Map<string, string> }) {
  return function () {
    return pick.map<Column>((v) => {
      if (v === 'name') {
        return NameColumn;
      } else if (v == 'class') {
        return ClassColumn;
      } else if (v === 'trade') {
        return {
          title: '行业',
          dataIndex: ['origin', 'trade'],
          width: nameWidth,
          ellipsis: true,
          render: (text, record) => (
            <span>
              {text
                ? text
                    .split(',')
                    .map((item: string) => columnsRender?.tradeMap.get(item))
                    .filter((itemText: string) => itemText)
                    .join(',')
                : text}
            </span>
          ),
        };
      } else if (v === 'isCharge') {
        return IsChargeColumn;
      } else if (v === 'update') {
        return UpdateTimeColumn;
      } else if (v === 'creat') {
        return CreatTimeColumn;
      }
      return {};
    });
  };
}
